// pages/create/calendar.js
var util = require('../../utils/util.js');
var activity_js = require('../../utils/activity.js');
var calendar_js = require('../../utils/calendar.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    owner: '',
    ownerid: 0,
    owner_list: [],
    owner_name_list: [],
    editable: false,
    id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    console.log("id:" + id);
    wx.setStorageSync("activity_create_type", 2);
    //获取组织信息
    var that = this;
    that.getOrganizations(wx.getStorageSync("yuyue_session"));
    that.setData({
      id: id,
    });
    if (id == 0){
      wx.setStorageSync("calendar.title", '');
      wx.setStorageSync("calendar.content", '');
      return false;
    }
    calendar_js.view(id,function (res){
        console.log(res.data);
        if (res.data.op == 'calendar_view') {
          var calendar = res.data.data.info;
          var editable = res.data.data.editable;
          wx.setStorageSync("now_edit_calendar", calendar);
          wx.setStorageSync("calendar.title", calendar.title);
          wx.setStorageSync("calendar.content", calendar.content);
          that.setData({
            type: calendar.type,
            title: calendar.title,
            content: calendar.content,
            owner_id: calendar.owner.id, 
            owner_name: calendar.owner.name,
            owner_avatar: calendar.owner.avatar, 
            editable: editable,
          });
        }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var title = wx.getStorageSync("calendar.title");
    var content = wx.getStorageSync("calendar.content");
    that.setData({
      title: title,
      content: content,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getOrganizations: function (yuyue_session) {
    var that = this;
    var orgs;
    var owner_list = [];
    var organizations = [];
    var orgs_name = [];
    var owner_name_list = [];
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatar = userInfo.avatarUrl
        var owner = new Object();
        owner.name = nickName;
        owner.avatar = avatar;
        owner.id = yuyue_session;
        owner.type = 1;
        owner_list.push(owner);
        owner_name_list.push(owner.name);

        util.organizations_request(function (data) {
          console.log(data);
          orgs = data;
          for (var org in orgs.own_orgs) {
            var owner = new Object();
            owner.name = orgs.own_orgs[org].name;
            owner.avatar = orgs.own_orgs[org].avatar_thumbnail_url;
            owner.id = orgs.own_orgs[org].id;
            owner.type = 2;
            owner_name_list.push(owner.name);
            owner_list.push(owner);

          }
          for (var org in orgs.join_orgs) {
            var owner = new Object();
            owner.name = orgs.join_orgs[org].name;
            owner.avatar = orgs.join_orgs[org].avatar_thumbnail_url;
            owner.id = orgs.join_orgs[org].id;
            owner.type = 2;
            owner_name_list.push(owner.name);
            owner_list.push(owner);
          }
          console.log(owner_list);
          console.log(owner_name_list);
          that.setData({
            owner_list: owner_list,
            owner_name_list: owner_name_list,
          });
        });
        return;
      }, fail(res) {
        util.neterror_Modal(function () {
          that.getOrganizations(yuyue_session);
          return;
        })
      }
    })
  }, 
  choose_owner: function (e) {
    var that = this;
    var id = that.data.id;
    if (id != 0) {
      return false;
    }
    var that = this;
    var owner_name_list = that.data.owner_name_list;
    wx.showActionSheet({
      itemList: owner_name_list,
      success: function (res) {
        that.setData({
          ownerid: res.tapIndex,
        })
      },
    })
  },
  modify_title: function () {
    if (this.is_editable() == false && this.data.id != 0) {
      return false;
    }
    wx.setStorageSync("editdetail_name", "日历活动标题");
    wx.setStorageSync("editdetail_var_name", "calendar.title");
    wx.setStorageSync("editdetail_value", wx.getStorageSync("calendar.title"));
    wx.navigateTo({
      url: 'edit_detail',
    })
  }, 
  modify_content: function () {
    console.log("do_modify");
    console.log(this.is_editable());
    console.log(this.data.id);
    if (this.is_editable() == false && this.data.id != 0) {
      return false;
    }
    wx.setStorageSync("editdetail_name", "日历活动描述");
    wx.setStorageSync("editdetail_var_name", "calendar.content");
    wx.setStorageSync("editdetail_value", wx.getStorageSync("calendar.content"));
    wx.navigateTo({
      url: 'edit_detail',
    })
  },
  create_calendar: function () {
    var that = this;
    var id = that.data.id;
    var title = that.data.title;
    var content = that.data.content;
    var ownerid = that.data.ownerid;
    var type = that.data.owner_list[ownerid].type;
    var owner = that.data.owner_list[ownerid].id;
    if (id > 0) {
      type = that.data.type;
      owner = that.data.owner_id;
    }
    console.log(id);
    console.log(title);
    console.log(content);
    console.log(type);
    console.log(owner);
    //return false;
    if (title == '' || content == '') {
      wx.showModal({
        title: '标题和说明请填写完整',
        showCancel: false
      });
      return false;
    }

    calendar_js.edit(id, title, content, type, owner, wx.getStorageSync("yuyue_session"),function (res){
      console.log(res);
      if (res.data.op == 'calendar_edit') {
        wx.setStorageSync("calendar.content", "");
        wx.setStorageSync("calendar.title", "");
        wx.navigateBack({});
      }
    });
  }, 
  manage_activity_list: function () {
    var that = this;
    var id = that.data.id;
    console.log(id);
    wx.navigateTo({
      url: '../create/calendar_list?id=' + id + "&type=" + that.data.type + "&owner=" + that.data.owner_id,
    })
  },
  is_editable: function () {
    if (this.data.editable == false) {
      return false;
    }else {
      return true;
    }
  }
})